// pages/shop/shop.ts
import { getShopList } from "../../utils/api";
import { getStorage } from "../../utils/util";
import Toast from '../../miniprogram_npm/@vant/weapp/toast/toast';
let page: number = 1;
let number: number = 10;
let X: number = 0;
let Y: number = 0;
let type: number = 0;
let count: number = 0;
let index = 0;
Page({
  data: {
    typeArr: [
      {
        id: 1,
        value: "店铺",
        isActive: true
      },
      // {
      //   id: 2,
      //   value: "收藏",
      //   isActive: false
      // }
    ],
    sortArr: [
      {
        id: 3,
        value: "按距离",
        isActive: true
      },
      {
        id: 2,
        value: "按评分",
        isActive: false
      }
    ],
    contentHeight: 0,
    list: []
  },
  onChangeType(event: any) {
    const targetId = event.currentTarget.dataset.id;
    const arr = this.data.typeArr;
    arr.forEach(item => {
      if (targetId == item.id) {
        item.isActive = true;
      } else {
        item.isActive = false;
      }
    })
    this.setData({
      typeArr: arr
    })
  },
  onChangeSort(event: any) {
    const targetId = event.currentTarget.dataset.id;
    if (targetId == 3) {
      type = 0
    }
    if (targetId == 2) {
      type = 1
    }
    page = 1;
    number = 10;
    this.onGetShopList();
    const arr = this.data.sortArr;
    arr.forEach(item => {
      if (targetId == item.id) {
        item.isActive = true;
      } else {
        item.isActive = false;
      }
    })
    this.setData({
      sortArr: arr
    })
  },
  onShow() {
    const BC = wx.getMenuButtonBoundingClientRect();
    const WC = wx.getWindowInfo();
    this.setData({
      contentHeight: WC.screenHeight - BC.top - BC.height - 110
    })
  },
  async onGetShopList() {
    if (!X && !Y) {
      const { userAddress_tb } = await getStorage('userInfo')
      X = userAddress_tb["longitude"];
      Y = userAddress_tb["latitude"];
    }
    const shopListRes = await getShopList({ page, number, X, Y, type })
    const { data } = shopListRes;
    count = data.count;
    data.rows.forEach((ele: any) => {
      ele["DIS"] = (ele["DIS"] / 1000).toFixed(2);
    });
    this.setData({
      list: data.rows
    })
  },
  onBreakPage(e: any) {
    const id = e.currentTarget.dataset.shopid;
    wx.navigateTo({
      url: `../shopDetails/shopDetails?id=${id}`
    })
  },
  // 下拉到底
  async onDownNext() {
    if (page * number > count) {
      Toast('🙂到底了哦~~🙂');
      return;
    }
    page++;
    if (!X && !Y) {
      const { userAddress_tb } = await getStorage('userInfo')
      X = userAddress_tb["longitude"];
      Y = userAddress_tb["latitude"];
    }
    const shopListRes = await getShopList({ page, number, X, Y, type })
    const { data } = shopListRes;
    count = data.count;
    data.rows.forEach((ele: any) => {
      ele["DIS"] = (ele["DIS"] / 1000).toFixed(2);
    });
    this.setData({
      list: [...this.data.list, ...data.rows]
    })
  },

  onLoad() {
    if (typeof this.getTabBar === 'function' && this.getTabBar()) {
      this.getTabBar().setData({
        selected: 1,
        show: true
      })
    }
    this.onGetShopList();
  },
})